package {
	import com.esria.samples.dashboard.managers.PodLayoutManager;
	import com.esria.samples.dashboard.view.ConsoleContent;
	import com.esria.samples.dashboard.view.GraphContent;
	import com.esria.samples.dashboard.view.ObjectiveGraphContent;
	import com.esria.samples.dashboard.view.ParetoGraphContent;
	import com.esria.samples.dashboard.view.Pod;
	import com.esria.samples.dashboard.view.PodContentBase;
	import com.esria.samples.dashboard.view.PopulationGraphContent;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	
	
	
	
	public class Task {
		
		public static const RUNNING:String = "Running";
		public static const STOPPED:String = "Stopped";
		
		private var _schemaName:String;
		private var _popName:String;
		private var _scriptName:String;
		
		public var id:int;
		public var state:String;		
		public var consoleManager:PodLayoutManager;
		public var graphManager:PodLayoutManager;
		
		public var consolePodContent:ConsoleContent;
		public var graphPodContents:ArrayCollection = new ArrayCollection(new Array(20));
		
		public function Task(id:int, _schemaName:String, _popName:String, _scriptName:String,
							 _consoleManager:PodLayoutManager, _graphManager:PodLayoutManager) {
			this.id = id;	
			this._schemaName = _schemaName;
			this._popName = _popName;
			this._scriptName = _scriptName;
			this.state = RUNNING;
			
			this.consoleManager = _consoleManager;
			this.graphManager = _graphManager;
			this.setConsolePod();
		}

		private function setConsolePod():void {
			var podContent:PodContentBase = new ConsoleContent();
			var pod:Pod = new Pod();
			pod.id = id.toString();
			pod.title = scriptName;
			pod.addChild(podContent);
			pod.addEventListener(FlexEvent.UPDATE_COMPLETE, onCreationCompleteConsolePod);
			consoleManager.addMinimizedItemAt(pod, -1);
			consolePodContent = podContent as ConsoleContent; 
		}

		private function addGraphPod(graphMsg:Object):void {
			var graphContent:GraphContent;
			
			trace(graphMsg.graph_type + " " + (graphMsg.graph_id).toString() + graphMsg.y_axis_name)
			
			switch(graphMsg.graph_type) {
				case GraphPanel.POP_GRAPH:
					graphContent = new PopulationGraphContent();
					trace("PopulationGraphContent has been instantiated");
					break;
				
				case GraphPanel.PARETO_GRAPH:
					graphContent = new ParetoGraphContent() ;
					trace("ParetoGraphContent has been instantiated");
					break;
					
				case GraphPanel.OBJ_GRAPH:
					graphContent = new ObjectiveGraphContent();
					trace("ObjectiveGraphContent has been started"); 
					break;				
			}

			graphContent.graphId = graphMsg.graph_id;
			graphContent.xAxisName = graphMsg.x_axis_name;
			graphContent.yAxisName = graphMsg.y_axis_name;
			graphContent.imageURL = graphContent.outputDir + "/" + this.schemaName + 
									"/" + this.popName + "/images/"; 

			graphContent.modelURL = graphContent.outputDir + "/" + this.schemaName + 
									"/" + this.popName + "/models/"; 

						
			var pod:Pod = new Pod();
			pod.id = id.toString();
			pod.title = graphMsg.title;
			pod.addChild(graphContent as PodContentBase);
			pod.addEventListener(FlexEvent.UPDATE_COMPLETE, onCreationCompleteGraphPod);
			graphManager.addMinimizedItemAt(pod, -1);
			
			trace("graph with id: " + graphContent.graphId.toString() + " has been added.");
			graphPodContents.addItemAt(graphContent, graphMsg.graph_id);
			
		}

		private function addGraphData(graphMsg:Object):void {
			var graphContent:GraphContent = graphPodContents[graphMsg.graph_id];
			graphContent.addData(graphMsg);
		}

		public function updateTask(task:Object):void {
			consolePodContent.updateConsole(task.console_out);
			if (task.state == STOPPED) {
				this.stopTask();
				return;
			}
			
			var graphMsgs:Array = task.graph_out;
			
			for (var i:Number = 0; i < graphMsgs.length; i++) {
				switch(graphMsgs[i].action) {
					case GraphPanel.CREATE_GRAPH:
						this.addGraphPod(graphMsgs[i]);
						break;
					case GraphPanel.ADD_DATA:
						this.addGraphData(graphMsgs[i]);
						break;
				}
			}
			
		}
		
		public function stopTask():void {
			state = STOPPED;
		}

		private function onCreationCompleteConsolePod(e:FlexEvent):void {
			e.currentTarget.removeEventListener(FlexEvent.UPDATE_COMPLETE, onCreationCompleteConsolePod);
			consoleManager.removeNullItems();
			consoleManager.updateLayout(false);				
		}

		private function onCreationCompleteGraphPod(e:FlexEvent):void {
			e.currentTarget.removeEventListener(FlexEvent.UPDATE_COMPLETE, onCreationCompleteGraphPod);
			graphManager.removeNullItems();
			graphManager.updateLayout(false);				
		}

		public function get schemaName():String {
			return _schemaName;
		}
		
		public function get popName():String {
			return _popName;
		}
		
		public function get scriptName():String {
			return _scriptName;
		}
	}
}